package io.gitee.skyarthur1987.leporidae.delayqueue.core;

import io.gitee.skyarthur1987.leporidae.delayqueue.dto.BatchDelayQueueTask;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.context.ApplicationEvent;

import java.util.Optional;

/**
 * 延迟队列拉取 事件
 * Created by skyarthur on 2023/1/27
 */
@Getter
@Setter
public class DelayQueuePollEvent extends ApplicationEvent {

    private BatchDelayQueueTask task;

    public DelayQueuePollEvent(Object source, BatchDelayQueueTask task) {
        super(source);
        this.task = task;
    }

    public String toPrint() {
        return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
                .append("topic", Optional.of(task).map(BatchDelayQueueTask::getTopic).orElse(null))
                .append("expireTime", Optional.of(task).map(BatchDelayQueueTask::getExpireTime).orElse(null))
                .append("task", Optional.of(task).map(x -> StringUtils.join(x.getTasks(), ",")).orElse(null))
                .toString();
    }
}
